package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.util.ConfigUtil;

import com.util.BODY;
import com.util.DateUtil;
import com.util.HttpRequest;
import com.util.JSONUtil;
import com.util.OpenKey;
import com.util.ReadConfig;
import com.util.SessionInfo;

/**
 * 缴存历史记录
 * @author 
 */
@Controller
@RequestMapping("/rydrcontroller")
public class Rydrcontroller {

	private static final Logger logger = Logger.getLogger(Rydrcontroller.class);
	Map map  = new HashMap();
	List list = new ArrayList();
	  
	@RequestMapping("/test")
	@ResponseBody
	public String entryinterface(HttpServletRequest request,HttpSession session, HttpServletResponse response , String limit ,String SFZH,String id,String start,String end) throws IOException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		String interfaceUrl = ReadConfig.readValue();
		Map reqMap = new HashMap();
		BODY b=new BODY();
		OpenKey p=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ172");
		//接口参数
		reqMap.put("LOGUserName","admin");
		reqMap.put("OpenKey",sessionIn.getKEY());
		b.setZJHM(SFZH);
		reqMap.put("BODY",b);
		String  result = p.getOpenKey(reqMap, session);
		Map retMap = JSONUtil.toMap(result);
		retMap = (Map) retMap.get("body");
    	List retlist = (List)retMap.get("RETDATA");
		logger.info("接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
			List listttmp = new ArrayList();
			for(int i=0;i<=retlist.size()-1;i++){
			  Map map=(Map)retlist.get(i);
				if(SFZH!=null && !SFZH.equals("")){
					if(map.get("ZJHM")==null || map.get("ZJHM").equals("")){
						continue;
					}else if(!SFZH.equals(map.get("ZJHM").toString())){
						continue;
						}
					}	
					listttmp.add(map);
				}				
				map.put("msg", resMap.get("RETURNDESC"));
				map.put("data", listttmp);
				
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
			}
		return null;
		
	}
	@RequestMapping("/dc")
	@ResponseBody
	public String tj(HttpServletRequest request,HttpSession session, HttpServletResponse response ,String DWJCBL,String SJHM,String GRJCJS,String GRJCBL, String limit ,String GRZH,String DRSJ,String YDW,String YDQ) throws IOException, ParseException{
		SessionInfo sessionIn = (SessionInfo) session.getAttribute(ConfigUtil.getSessionInfoName());
		//读取配置文件路劲
		System.out.println(DateUtil.getFormatDate(new Date(), "yyyy/MM/dd HH:mm:ss"));
		String interfaceUrl = ReadConfig.readValue();
		if(YDQ!=null && YDQ!="" ){
			YDQ =new String(YDQ.getBytes("ISO-8859-1"),"UTF-8");
		}
		Map reqMap = new HashMap();
		BODY b=new BODY();
		OpenKey p=new OpenKey();
		//接口编码
		reqMap.put("AUTHORITY", "YBGJJ162");
		//接口参数
		
		reqMap.put("LOGUserName","admin");
		reqMap.put("OpenKey",sessionIn.getKEY());
		b.setGRZH(GRZH);
		b.setDRDQ(sessionIn.getDQM());
		b.setYDQ(YDQ);
		b.setDRDW(sessionIn.getDWZH());
		b.setYDW(YDW);
		reqMap.put("BODY",b);
		String  result = p.getOpenKey(reqMap, session);
		Map retMap = JSONUtil.toMap(result);

		retMap = (Map) retMap.get("body");
		logger.info("接口返回内容:"+result);
		if(result != null){
			Map resMap = JSONUtil.toMap(result);
				//List resList = JSONUtil.toList(resMap.get("body").toString());
				//map.put("count", resList.size());
				map.put("code", 0); 
				map.put("msg", resMap.get("RETURNDESC"));
				map.put("data", retMap);
				
				response.setHeader("Content-type", "text/html;charset=UTF-8");
				response.setCharacterEncoding("UTF-8");
				String jsonString = JSONUtil.ToJsongString2(map);
				PrintWriter pw = response.getWriter();
				pw.write(jsonString);
		}
		return null;
	}
}
